package com.example.dbcms.service.impl;

import com.example.dbcms.domain.User;
import com.example.dbcms.mapper.ResidentDao;
import com.example.dbcms.mapper.RoleDao;
import com.example.dbcms.mapper.UserDao;
import com.example.dbcms.service.UserService;
import com.example.dbcms.utils.DateUtil;
import com.example.dbcms.utils.FileUploadUtil;
import com.example.dbcms.utils.ResultVoUtil;
import com.example.dbcms.utils.StringUtil;
import com.example.dbcms.vo.ResultVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private ResidentDao residentDao;

    /**
     * 文件上传保存地址
     */
    @Value("${project.upload.file-path}")
    String file_path;

    @Override
    public PageInfo<User> selectAllUser(User user) {
        PageHelper.startPage(user.getPageNum(),10);
        PageInfo<User> userPageInfo = new PageInfo<>(userDao.selectAllUser(user));
        return userPageInfo;
    }

    @Override
    public int deleteUserById(int id) {
        return userDao.deleteUserById(id);
    }

    @Override
    public int deleteMultipleUserById(List<Integer> ids) {
        return userDao.deleteMultipleUserById(ids);
    }

    @Override
    public User getUserById(int id) {
        return userDao.getUserById(id);
    }

    @Override
    public int updateUserById(User user) {
        return userDao.updateUserById(user);
    }

    @Override
    public int addUser(User user) {
        user.setId(userDao.getMaxId()+1);
        user.setPassword(StringUtil.getPassword());
        if(user.getUserRole().equals("普通用户")){
            user.setUserId(residentDao.getResidentId(user.getBuildingNo(),user.getRoomNo()));
        }
        user.setUserRoleId(roleDao.getRoleIdByName(user.getUserRole()));
        user.setCreateTime(DateUtil.getCurrentDate());
        user.setPicture(StringUtil.DEFAULT_PICTURE_URL);
        return userDao.addUser(user);
    }

    @Override
    public User getUserByUserAccount(String account) {
        return userDao.getUserByUserAccount(account);
    }

    @Override
    public ResultVo updatePicture(MultipartFile file,String userAccount) {
        String uuid = UUID.randomUUID()+"";
        ResultVo resultVo = FileUploadUtil.pictureUpload(file, file_path, uuid);
        if(resultVo.getCode()==200){
            int result = userDao.updatePicture(userAccount, resultVo.getMsg());
            if (result == 1){
                return resultVo;
            }else {
                return ResultVoUtil.error("修改失败");
            }
        }
        return resultVo;
    }

    @Override
    public ResultVo editPassword(String original, String password, String confirm) {
        String userAccount = (String) SecurityUtils.getSubject().getPrincipal();//获得账号
        User userByUserAccount = getUserByUserAccount(userAccount);
        if(!userByUserAccount.getPassword().equals(original)){
            return ResultVoUtil.error("原始密码错误");
        }
        if(password==null||confirm==null||!password.equals(confirm)){
            return ResultVoUtil.error("新密码有误");
        }
        int result = userDao.editPassword(userByUserAccount.getId(), password);
        return result==1?ResultVoUtil.success("修改密码成功"):ResultVoUtil.error("修改密码失败");
    }

}
